package com.pocketsweet.ui.uilib.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.pocketsweet.R;
import com.pocketsweet.C;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.model.MLUserLike;
import com.pocketsweet.service.UserService;
import com.pocketsweet.utils.StringUtils;

/**
 * 
 * @author Kam
 * 
 */
public class FollowListAdapter extends BaseListAdapter<MLUserLike> {
	private Context context;

	public FollowListAdapter(Context ctx) {
		super(ctx);
		this.context = ctx;
	}

	public FollowListAdapter(Context ctx, List<MLUserLike> datas) {
		super(ctx, datas);
		this.context = ctx;
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_follow, parent, false);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();

		}
		viewHolder.refreshView(datas.get(position), position);
		return convertView;
	}

	public class ViewHolder {
		ImageView imgAvatar;
		TextView tvuserName;
		TextView tvUserAge;
		TextView tvFrom;
		TextView tvType;
		TextView tvStar;
		MLUserLike item;
		MLUser mlUser;
		int likeCount = 0;
		public View parent;

		public ViewHolder(View parent) {
			this.parent = parent;
			initView(parent);
		}

		public void initView(View parent) {
			imgAvatar = (ImageView) parent.findViewById(R.id.imgAvatar);
			tvuserName = (TextView) parent.findViewById(R.id.tvuserName);
			tvUserAge = (TextView) parent.findViewById(R.id.tvUserAge);
			tvFrom = (TextView) parent.findViewById(R.id.tvFrom);
			tvStar = (TextView) parent.findViewById(R.id.tvStar);
			tvType = (TextView) parent.findViewById(R.id.tvType);
		}

		public void refreshView(MLUserLike item, int i) {
			this.item = item;
			// 设置图片
			mlUser = item.getUser();
			if (mlUser != null) {
				String avatar = mlUser.getAvatarUrl();
				if (avatar != null && !avatar.equals("")) {
					UserService.displaySquare(avatar, imgAvatar);
				} else {
					imgAvatar.setImageResource(R.drawable.default_user_avatar);
				}
				if (!StringUtils.isEmpty((mlUser.getNickname()))) {
					tvuserName.setText(mlUser.getNickname());
				}
				if (!StringUtils.isEmpty((mlUser.getBelong()))) {
					tvType.setText(mlUser.getBelong());
				}
				// 设置年龄
				if (!StringUtils.isEmpty(String.valueOf(mlUser.getAgeNum()))) {
					tvUserAge.setText(String.valueOf(mlUser.getAgeNum()));
				}
				// 设置性别颜色
				if (mlUser.getGender() == 1) {
					tvUserAge
							.setBackgroundResource(R.drawable.shape_rectangle_bule);
				} else if (mlUser.getGender() == 2) {
					// 设置颜色
					tvUserAge
							.setBackgroundResource(R.drawable.shape_rectangle_red);
				}
				// 设置星座
				tvStar.setText(mlUser.getConstellation());
				// 设置地理位置
				String location = mlUser.getLocationCity();
				if (StringUtils.isEmpty(location)) {
					location = mlUser.getLocationProvince();
				}
				tvFrom.setText(location);
			}
		}

	}

}
